package com.ll.domain.user.convert;

import com.alibaba.fastjson.JSONObject;
import com.ll.base.util.ScalarUtil;
import com.ll.domain.user.enums.RoleEnum;
import com.ll.dal.po.UserPO;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiV2UserListResponse;
import com.ll.domain.user.dto.UserDTO;

import java.util.Arrays;
import java.util.List;

public class UserConvert {
    private static final Long  DEFAULT_TENANT_ID = 1L;

    public static UserDTO PO2DTO(UserPO po) {
        if(po == null){
            return null;
        }
        List<RoleEnum> roles = JSONObject.parseArray(po.getRoles(),RoleEnum.class);
        List<Long> deptIds = JSONObject.parseArray(po.getExtData(), Long.class);
        RoleEnum maxRole = RoleEnum.getMaxRole(roles);
        return UserDTO.builder()
                .id(po.getId())
                .dingTalkUserId(po.getDingUserId())
                .deptIds(deptIds)
                .logo(po.getLogo())
                .userName(po.getName())
                .integral(po.getIntegral())
                .roles(roles)
                .roleDic(maxRole.getRoleName())
                .roleCode(maxRole.ordinal())
                .tenantId(po.getTenantId())
                .build();
    }

    public static UserPO DTO2PO(UserDTO dto) {
        UserPO po = new UserPO();
        po.setId(dto.getId());
        po.setDingUserId(dto.getDingTalkUserId());
        po.setIntegral(ScalarUtil.DEFAULT_INTEGER_VALUE);
        po.setName(dto.getUserName());
        po.setStatus("正常");
        po.setLogo(dto.getLogo());
        po.setTenantId(dto.getTenantId());
        po.setRoles(JSONObject.toJSONString(dto.getRoles()));
        po.setExtData(JSONObject.toJSONString(dto.getDeptIds()));
        return po;
    }

    public static UserDTO respUserGet2DTO(OapiUserGetResponse response) {
        List<RoleEnum> roles = RoleEnum.getByRole(response.getDepartment());
        RoleEnum maxRole = RoleEnum.getMaxRole(roles);
        return UserDTO.builder()
                .tenantId(DEFAULT_TENANT_ID)
                .logo(response.getAvatar())
                .dingTalkUserId(response.getUserid())
                .deptIds(response.getDepartment())
                .userName(response.getName())
                .roles(roles)
                .roleDic(maxRole.getRoleName())
                .roleCode(maxRole.ordinal())
                .build();
    }


    public static UserDTO respUserList2DTO(OapiV2UserListResponse.ListUserResponse response){
        List<RoleEnum> roles = RoleEnum.getByRole(response.getDeptIdList());
        RoleEnum maxRole = RoleEnum.getMaxRole(roles);
        return UserDTO.builder()
                .logo(response.getAvatar())
                .dingTalkUserId(response.getUserid())
                .deptIds(response.getDeptIdList())
                .userName(response.getName())
                .roles(roles)
                .tenantId(DEFAULT_TENANT_ID)
                .roleDic(maxRole.getRoleName())
                .roleCode(maxRole.ordinal())
                .build();

    }

    public static UserDTO getSysUser(String dingTalkUserId) {
        if ("system".equals(dingTalkUserId)) {
            return UserDTO.builder()
                    .tenantId(DEFAULT_TENANT_ID)
                    .dingTalkUserId("system")
                    .deptIds(Arrays.asList(467725548L))
                    .userName("系统管理员")
                    .roles(Arrays.asList(RoleEnum.管理员))
                    .roleDic(RoleEnum.管理员.getRoleName())
                    .roleCode(RoleEnum.管理员.ordinal())
                    .build();
        }
        return null;
    }

}
